package cn.tinlan.shopcoin.app.mine;

import android.Manifest;
import android.content.ActivityNotFoundException;
import android.content.Intent;

import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.FileProvider;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.FileUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.yalantis.ucrop.UCrop;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.CallBackProxy;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;

import org.json.JSONArray;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.tinlan.shopcoin.R;
import cn.tinlan.shopcoin.Utils.SPUtils;
import cn.tinlan.shopcoin.Utils.ToastUtils;
import cn.tinlan.shopcoin.app.home.Model.IconTitleModel;
import cn.tinlan.shopcoin.app.home.adapter.LittleModuleAdapter;
import cn.tinlan.shopcoin.app.home.decoration.HomeGridDecoration;
import cn.tinlan.shopcoin.app.login.PerfectUserInfoActivity;
import cn.tinlan.shopcoin.app.login.UserModel;
import cn.tinlan.shopcoin.app.mine.order.OrderActivity;
import cn.tinlan.shopcoin.app.mine.order.OrderDetailActivity;
import cn.tinlan.shopcoin.app.mine.set.MineSettingActivity;
import cn.tinlan.shopcoin.app.mine.set.MineWalletActivity;
import cn.tinlan.shopcoin.app.mine.view.PersonalItemView;
import cn.tinlan.shopcoin.base.BaseFragment;
import cn.tinlan.shopcoin.base.CustomApiResult;
import cn.tinlan.shopcoin.widget.category.Constants;
import pub.devrel.easypermissions.EasyPermissions;

import static android.app.Activity.RESULT_OK;

/**
 * Created by Mortimer on 2018/11/20. 15:01
 * 佛祖🙏，永无bug。
 */
public class MineFragment extends BaseFragment implements View.OnClickListener {

    @BindView(R.id.avatar)
    ImageView avatar;

    @BindView(R.id.personal_name)
    TextView personal_name;

    @BindView(R.id.mine_perfect_tv)
    TextView mine_perfect_tv;

    @BindView(R.id.recycler_order)
    RecyclerView recycler_order;

    @BindView(R.id.setting)
    View setting;
    @BindView(R.id.mwallet)
    PersonalItemView mwallet;

    @BindView(R.id.mine_see_allorder)
    TextView see_allorder;

    @BindView(R.id.mpoint)
    View mPoint;



//    @Nullable
//    @Override
//    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
////        View homeView = inflater.inflate(R.layout.fragment_mine,null);
//        if (rootView == null) {
//            rootView = inflater.inflate(R.layout.fragment_mine, null);
//            Log.i("mine","rootview");
//        }
//        //绑定 ButterKnife
//        ButterKnife.bind(this, rootView);
//        return  rootView;
//    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    @Override
    protected void onFragmentFirstVisible() {


    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void loadData() {

    }


    @Override
    public void initView(View rootView) {
        super.initView(rootView);

        initHeadView();

        initOrderRecyclerView();
        initOtherViews();
    }

    private void initHeadView() {
        UserModel userModel =  SPUtils.getUserBean(mActivity);
        if (!userModel.getMobile().isEmpty()){
            personal_name.setText(userModel.getMobile());
        }

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden){
//            Log.d("minehidden","show11111");
            initHeadView();
        }
    }

    private void initOtherViews() {
        setting.setOnClickListener(this);
        mwallet.setOnClickListener(this);
        see_allorder.setOnClickListener(this);
        mPoint.setOnClickListener(this);
        mine_perfect_tv.setOnClickListener(this);
    }


    //设置分类
    private void initOrderRecyclerView() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(),5);
        //设置LayoutManager
        recycler_order.setLayoutManager(gridLayoutManager);
        //设置分割器
        recycler_order.addItemDecoration(new HomeGridDecoration(6));
        //设置动画
        recycler_order.setItemAnimator(new DefaultItemAnimator());

        final List<IconTitleModel> datas = new ArrayList<>();
        datas.add(new IconTitleModel(R.drawable.waitpay, "待付款"));
        datas.add(new IconTitleModel(R.drawable.waitget, "待收货"));
        datas.add(new IconTitleModel(R.drawable.waitp, "待评价"));
        datas.add(new IconTitleModel(R.drawable.zuomeng, "售后"));
        datas.add(new IconTitleModel(R.drawable.backmoney, "退款"));

        //设置Adapter
        LittleModuleAdapter littleModuleAdapter = new LittleModuleAdapter(
                R.layout.view_icon_title_small, datas);
        recycler_order.setAdapter(littleModuleAdapter);

        //设置item点击事件
        littleModuleAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                ToastUtils.show(datas.get(position).getTitle());
//                startActivity(new Intent(mActivity, OrderActivity.class));

                if (position == 3 || position == 4) {
                    ToastUtils.show("功能暂无");
                }else {
                    Intent intent = new Intent(mActivity,OrderActivity.class);
                    intent.putExtra("position",position+1);
                    startActivity(intent);
                }

            }
        });
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.setting:
                startActivity(new Intent(mActivity, MineSettingActivity.class));
                break;
            case R.id.mwallet:
                startActivity(new Intent(mActivity, MineWalletActivity.class));
//                createOrder();
                break;
            case R.id.mine_see_allorder:
                Intent intent = new Intent(mActivity,OrderActivity.class);
                intent.putExtra("position",0);
                startActivity(intent);
                break;
            case R.id.mpoint:
                Intent point = new Intent(mActivity,PointActivity.class);
//                point.putExtra("position",0);
                startActivity(point);
                break;
            case R.id.mine_perfect_tv:
                Intent perfect = new Intent(mActivity,PerfectUserInfoActivity.class);
//                point.putExtra("position",0);
                startActivity(perfect);
                break;
        }

    }


//    private void createOrder() {
//
//        HttpParams params = new HttpParams();
//        params.put("user_id", SPUtils.getUserID(mActivity));
//        params.put("address_id","1");
//
//        StringBuilder cartList = new StringBuilder();
////        JSONArray jsonArray = new JSONArray();
//        Map<String ,Object> map2 =new HashMap<>();
////        for (int i = 0; i<5;i++) {
////            Map<String ,String> map1 =new HashMap<>();
////            map1.put("goods_id","1");
////            map1.put("stock_id","1");
////            map1.put("buy_num","1");
//////            JSONArray json = new JSONArray();
//////            json.put(1);
//////            json.put(2);
//////            json.put(3);
////            map2.put(""+i,map1);
//////            jsonArray.put(json);
//////            cartList.append("goods_id=>1@"+"stock_id=>1@"+"buy_num=>1"+",");
////        }
//        params.put("cart",map2.toString());
//        params.put("remark","1");
//        params.put("mtype","1");
//
//        EasyHttp.post(Constants.OrderCreate)
//                .params(params)
//                .execute(new CallBackProxy<CustomApiResult<String>, String>(new SimpleCallBack<String>() {
//                    @Override
//                    public void onError(ApiException e) {
//                        //请求错误
//                    }
//
//                    @Override
//                    public void onSuccess(String response) {
////                        ToastUtils.show("");
//
//                    }
//
//                }) {
//                });
//    }



}
